package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.RecommendService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@DubboService
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //推荐今日最佳
    @Override
    public RecommendUser findRecommendUser(Long userId) {
        Query query = new Query(Criteria.where("toUserId").is(userId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);

        return  mongoTemplate.findOne(query, RecommendUser.class);
    }


    //推荐列表
    @Override
    public PageBeanVo findRecommendUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1)*pageSize + 1;
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(index).limit(pageSize);
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        long count = mongoTemplate.count(query, RecommendUser.class) - 1L;

        return new PageBeanVo(pageNum,pageSize,count,recommendUserList);
    }

    //推荐用户详情
    @Override
    public RecommendUser findRecommendUserDetails(Long userId, Long toUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("toUserId").is(toUserId));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //删除卡片
    @Override
    public void removeCard(Long toUserId, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("toUserId").is(toUserId));
        mongoTemplate.remove(query, RecommendUser.class);
    }
}
